<?php
class AdminGroup extends A_Content {
	
	private $_group_list = array();
	
	public function __construct($top,$parent) {
		parent::__construct($top,$parent);
		
		$get_groups = $this->_top->_sql->exe("SELECT `group_id` FROM `".TAB_PREF."admin_group` WHERE 1");
		for ($g=0;$g<count($get_groups);$g++) {
			array_push($this->_group_list,$get_groups[$g]['group_id']);
		}
	}
	
	public function length() {
		return count($this->_group_list);
	}
	
	/**
	 * Returns pointer to SingleAdminGroup class
	 *
	 * @param int $idx - index of administrator group note in found list
	 * @return SingleAdminGroup
	 */
	public function item($idx) {
		return $this->getById($this->_group_list[$idx]);
	}
	
	public function __get($param) {
		if ($param=='length') {
			return $this->length();
		}
	}
	
	/**
	 * Return pointer to SingleAdminGroup class by ID
	 *
	 * @param int $id - admin id
	 * @return SingleAdminGroup
	 */
	public function getById($id) {
		if (!isset($this->_cashe_obj[$id])) {
			$this->_cashe_obj[$id] = new SingleAdminGroup($this->_top,$this,$id);
		}
		
		return $this->_cashe_obj[$id]; 
	}
	
	/**
	 * Adds new administrator group and returns pointer to it's class
	 *
	 * @return SingleAdminGroup
	 */
	public function add()
	{
		$this->_top->_sql->exeDummy("DELETE FROM `".TAB_PREF."admin_group` WHERE `name`=''");
		$this->_top->_sql->exeDummy('INSERT INTO `'.TAB_PREF.'admin_group` (add_date) VALUES (NOW());');
		$id = $this->_top->_sql->last_insert_id();
		return $this->getById($id);
	}
	
	/**
	 * Returns pointer to SearchAdminGroup class
	 *
	 * @return SearchAdminGroup
	 */
	public function search() {
		return new SearchAdminGroup($this->_top,$this);
	}
	
/**
	 * Removes all priveleges from menu point
	 *
	 * @param MenuItem|Integer $menu - пункт меню
	 */
	public function clearPrev($menu) {
		if (gettype($menu)!='object') {
			$menu = $this->_top->Menu->getPointById($menu);
		}
		for ($g=0;$g<$this->length();$g++) {
			$this->item($g)->unlinkMenu($menu);
		}
	}
	
	/**
	 * Copies menu permissions from parent point
	 *
	 * @param MenuItem|Integer $menu - пункт меню
	 */
	public function copyPrev($menu) {
		if (gettype($menu)!='object') {
			$menu = $this->_top->Menu->getPointById($menu);
		}
		
		for ($g=0;$g<$this->length();$g++) {
			if ($this->item($g)->checkMenu($menu->parent)) {
				$this->item($g)->linkMenu($menu);
			}
		}
	}
}

class SearchAdminGroup extends A_Content {
	
	public $name;
	public $admin_id;
	
	private $_group_list = array();
	
	/**
	 * Starts search engine and return itself
	 *
	 * @return self
	 */
	public function Make() {
		
		$tables = '';
		$where = '';
		
		if ($this->admin_id!=null) {
			$tables .= ', `'.TAB_PREF.'administrator2group` as l';
			$where .= 'AND g.`group_id`=l.`group_id` AND l.`administrator_id`='.$this->admin_id;
		}
		
		if ($this->name!=null) {
			$where .= ' AND LOWER(g.`name`) LIKE LOWER("'.$this->_top->Utils->prepareValueForSQL($this->name).'%")';
		}
		
		$sql = "SELECT g.`group_id` FROM `".TAB_PREF."admin_group` as g {$tables} WHERE 1 {$where}";
		$get_groups = $this->_top->_sql->exe($sql);
		for ($g=0;$g<count($get_groups);$g++) {
			array_push($this->_group_list,$get_groups[$g]['group_id']);
		}
		
		return $this;
	}
	
	public function length() {
		return count($this->_group_list);
	}
	
	/**
	 * Returns pointer to SingleAdminGroup class
	 *
	 * @param int $idx - index of administrator note in found list
	 * @return SingleAdminGroup
	 */
	public function item($idx) {
		return $this->_parent->getById($this->_group_list[$idx]);
	}
	
	public function __get($param) {
		if ($param=='length') {
			return $this->length();
		}
	}
	
	/**
	 * Return pointer to SingleAdminGroup class by ID
	 *
	 * @param int $id - admin id
	 * @return SingleAdminGroup
	 */
	public function getById($id) {
		$this->_parent->getById($id);
	}
	
	/**
	 * Adds new administrator and returns pointer to it's class
	 *
	 * @return SingleAdminGroup
	 */
	public function add() {
		$this->_parent->add();
	}
}

class SingleAdminGroup extends A_SingleContent {
	
	private 
		$_modules = array(),
		$_groups = array(),
		$_menu = array();
	
	protected function loader() {
		$get_data = $this->_top->_sql->exe("SELECT * FROM `".TAB_PREF."admin_group` WHERE `group_id`={$this->_content_id}");
		
		if (count($get_data)==0)
			throw $this->_top->Utils->PushError("Administrator group Id: {$this->_content_id} not found","Системная ошибка. Обратитесь к администратору");
		else {
			$this->_data = $get_data[0];
		}
		return true;
	}
	
	public function __set($param,$value) {
		if ($param=='name' && $this->name!=$value) {
			$checkName = $this->_top->AdminGroup->search();
			$checkName->name = $value;
			$checkName->Make();
			if ($checkName->length()!=0) {
				throw $this->_top->Utils->PushError('Name is not unique','Название группы не уникально.');
			}
		} elseif ($param=='is_blocked' && ($value!='1' && $value!='0')) {
			throw $this->_top->Utils->PushError('Trying to set illegal Administrator blocked value','Системная ошибка. Обратитесь к разработчикам');
		}
		
		
		$this->_data[$param] = $value;
		
		$value = $this->_top->Utils->prepareValueForSQL($value);
		
		$this->_top->_sql->exeDummy("UPDATE `".TAB_PREF."admin_group` SET `$param`='$value' WHERE `group_id`={$this->_content_id}");
	}
	
	protected function deleter() {
		$this->_top->_sql->exeDummy("DELETE FROM `".TAB_PREF."admin_group` WHERE `group_id`='{$this->_content_id}'");
		$this->_top->_sql->exeDummy("DELETE FROM `".TAB_PREF."administrator2group` WHERE `group_id`='{$this->_content_id}'");
		$this->_top->_sql->exeDummy("DELETE FROM `".TAB_PREF."admin_group2menu` WHERE `group_id`='{$this->_content_id}'");
		$this->_top->_sql->exeDummy("DELETE FROM `".TAB_PREF."admin_group2modul` WHERE `group_id`='{$this->_content_id}'");
		
		unset($this->_data);
	}
	
	
	public function __get($param) {
		return $this->_data[$param];
	}
	
	/**
	 * Returns administrators list
	 * 
	 * @return SearchAdministrator
	 *
	 */
	function adminList() {
		$get_admins = $this->_top->Administrator->search();
		$get_admins->group_id = $this->_content_id;
		return $get_admins->Make();
	}
	
	
	/**
	 * Links group to admin
	 *
	 * @param integer|SingleAdministrator $admin
	 */
	public function linkAdmin($admin) {
		if (gettype($admin)!='object') {
			$admin = $this->_top->Administrator->getById($admin);
		}
		$admin->linkGroup($this);
	}
	
	/**
	 * Unlinks admin from group
	 *
	 * @param integer|SingleAdministrator $admin
	 */
	public function unlinAdmin($admin) {
		if (gettype($admin)!='object') {
			$admin = $this->_top->Administrator->getById($admin);
		}
		$admin->unlinkGroup($this); 
	}
	
	/**
	 * Enter description here...
	 *
	 * @param SingleAdministrator|integer|string $admin
	 */
	public function checkAdmin($admin) {
		if (gettype($admin)!='object') {
			$admin = $this->_top->Administrator->getById($admin);
		}
		return $admin->checkGroup($this);
	}
	
	/**
	 * Adds rights to manage module
	 *
	 * @param string $key - module key name
	 */
	public function linkModule($key) {
		if (!$this->checkModule($key)) {
			$this->_modules[$key] = true;
			$this->_top->_sql->exeDummy("INSERT INTO `".TAB_PREF."admin_group2module` (`group_id`,`module`)
				VALUES ({$this->_content_id},'{$key}')");
		}
	}
	
	/**
	 * Removes rights to manage module
	 *
	 * @param string $key - module key name
	 */
	public function unlinkModule($key) {
		$this->_modules[$key] = false;
		$this->_top->_sql->exeDummy("DELETE FROM `".TAB_PREF."admin_group2module`
			WHERE `group_id`={$this->_content_id} AND `module`='{$key}'");
	}
	
	
	/**
	 * Checks if administrator has rights to manage module
	 *
	 * @param string $key - module key name
	 */
	public function checkModule($key) {
		if (!isset($this->_modules[$key])) {
			$check_module = $this->_top->_sql->exe("SELECT * FROM `".TAB_PREF."admin_group2module` 
				WHERE `group_id`={$this->_content_id} AND `module`='{$key}'");
			$this->_modules[$key] = count($check_module)==0 ? false : true;
		}
		
		return $this->_modules[$key];
	}
	
	/**
	 * Adds rights to manage menu point
	 *
	 * @param SingleMenu|String $menu
	 */
	public function linkMenu($menu) {
		if (gettype($menu)=='object') {
			$menu = $menu->menu_id;
		}
		
		if (!$this->checkMenu($menu)) {
			$this->_menu[$menu] = true;
			$this->_top->_sql->exeDummy("INSERT INTO `".TAB_PREF."admin_group2menu` (`group_id`,`menu_id`)
				VALUES ({$this->_content_id},'{$menu}')");
		}
	}
	
	/**
	 * Removes rights to manage menu point
	 *
	 * @param SingleMenu|String $menu
	 */
	public function unlinkMenu($menu) {
		if (gettype($menu)=='object') {
			$menu = $menu->menu_id;
		}
		
		$this->_menu[$menu] = false;
		$this->_top->_sql->exeDummy("DELETE FROM `".TAB_PREF."admin_group2menu`
			WHERE `group_id`={$this->_content_id} AND `menu_id`='{$menu}'");
	}
	
	/**
	 * Checkin if admin has rights to manage menu
	 *
	 * @param SingleMenu|String $menu
	 * @return boolean
	 */
	public function checkMenu($menu) {
		if (gettype($menu)=='object') {
			$menu = $menu->menu_id;
		}
		
		if (!isset($this->_menu[$menu])) {
			$check_menu = $this->_top->_sql->exe("SELECT * FROM `".TAB_PREF."admin_group2menu` 
				WHERE `group_id`={$this->_content_id} AND `menu_id`='{$menu}'");
			$this->_menu[$menu] = count($check_menu)==0 ? false : true;
		}
		
		return $this->_menu[$menu];
	}
}